package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.Persona;


@WebServlet("/PersonaHandler")
public class PersonaHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	private static String ADD = "/PersonaAdd.jsp";
    private static String Edit = "/Personaedit.jsp";
    private static String PersonaRecord="/ListPersona.jsp";
    private PersonaDao dao;
       
    public PersonaHandler() {
        super();
        dao = new PersonaDao();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";       
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listPersona")) {
			redirect = PersonaRecord;
			request.setAttribute("Persona", dao.getAllPersonas());
		}
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("PersonaId");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Persona objPersona = new Persona();
			
			objPersona.setNombre(request.getParameter("Nombres"));
			objPersona.setApellido(request.getParameter("Apellidos"));
			objPersona.setSexo(request.getParameter("Sexo"));
			objPersona.setDireccion(request.getParameter("Direccion"));
			objPersona.setCorreos(request.getParameter("Correos"));
			objPersona.setIdentificacion(Integer.parseInt(request.getParameter("Identificacion")));
			objPersona.setTipo_Indentificacion(request.getParameter("tip_inden"));
			
			dao.addPersona(objPersona);
			redirect = PersonaRecord;
			request.setAttribute("Persona", dao.getAllPersonas());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String Persona_Id = request.getParameter("PersonaId");
			int lugid = Integer.parseInt(Persona_Id);
			dao.removePersona(lugid);
			redirect = PersonaRecord;
			request.setAttribute("Persona", dao.getAllPersonas());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String Persona_Id = request.getParameter("PersonaId");
			int lugid = Integer.parseInt(Persona_Id);
			Persona objPersona = new Persona();
			objPersona.setId(lugid);
			objPersona.setNombre(request.getParameter("Nombres"));
			objPersona.setApellido(request.getParameter("Apellidos"));
			objPersona.setSexo(request.getParameter("Sexo"));
			objPersona.setDireccion(request.getParameter("Direccion"));
			objPersona.setCorreos(request.getParameter("Correos"));
			request.setAttribute("Persona", objPersona);
			redirect = PersonaRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listPersona")) {
			redirect = PersonaRecord;
			request.setAttribute("Persona", dao.getAllPersonas());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
